/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerz.component;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import towerz.system.Utilities;

/**
 *
 * @author LegACy
 */
public class ButtonWheel {
	public ButtonWheel(int limit, int selected) {
		//Initialize
		m_X			= 0;
		m_Y			= 0;
		m_Index		= 0;
		m_Buttons	= new Vector();
		m_Draw		= true;
		
		//Save
		m_Limit		= limit;
		m_Selected	= selected;
		
		try {
			m_Selection1 = Image.createImage(Utilities.INTERFACE_FOLDER + "/selection1.png");
			m_Selection2 = Image.createImage(Utilities.INTERFACE_FOLDER + "/selection2.png");
		} catch (IOException ex) {}
	}
	
	public Button getSelectedButton() {
		return (Button)m_Buttons.elementAt(m_Index);
	}
	
	public void addButton(int id, String text) {
		//Create and add button
		m_Buttons.addElement(new ButtonPush(text, id, 0, 0));
		((ButtonPush)m_Buttons.lastElement()).setAlignment(Graphics.HCENTER | Graphics.TOP);
		((ButtonPush)m_Buttons.lastElement()).setPush(false);
	}
	
	public void setPosition(int x, int y) {
		//Set position
		m_X = x;
		m_Y = y;
		
		//Draws
		m_Draw = true;
	}
	
	public void setSelectedID(int id) {
		//Find button id
		for (int i = 0; i < m_Buttons.size(); i++)
			if (((Button)m_Buttons.elementAt(i)).getID() == id) m_Index = i;
	}
	
	public void forceDraw() {
		//Draws when told
		m_Draw = true;
	}
	
	public void update(int keys) {
		//Change selection based on key
		if ((keys & GameCanvas.UP_PRESSED) != 0)	m_Index--;
		if ((keys & GameCanvas.DOWN_PRESSED) != 0)	m_Index++;

		//Correct selection
		if (m_Index < 0) m_Index = m_Buttons.size() - 1;
		m_Index = m_Index % m_Buttons.size();

		//Restart state
		for (int i = 0; i < m_Buttons.size(); i++) {
			((Button)m_Buttons.elementAt(i)).setState(Button.STATE_NONE);
		}

		//Manage the selected button state
		((Button)m_Buttons.elementAt(m_Index)).setState(Button.STATE_SELECTED);
	}
	
	public void draw(Graphics g) {
		//Skip if shouldn't be drawn
		if (!m_Draw) return;
		
		//Get start and end
		int Start	= m_Index - m_Selected;
		int End		= Start + m_Limit;
		if (m_Limit > m_Buttons.size()) End = Start - 1;
		
		//Draws selection
		g.drawImage(m_Selection1, m_X - (getSelectedButton().getWidth() / 2) - 8, m_Y + (24 * 2) + (getSelectedButton().getHeight() / 2) - 2, Graphics.RIGHT | Graphics.VCENTER);
		g.drawImage(m_Selection2, m_X + (getSelectedButton().getWidth() / 2) + 6, m_Y + (24 * 2) + (getSelectedButton().getHeight() / 2) - 2, Graphics.LEFT | Graphics.VCENTER);
		
		//For each button
		for (int i = Start; i < End; i++) {
			//Get button index
			int Index = i;
			if (Index < 0)					Index = Index + m_Buttons.size();
			if (Index >= m_Buttons.size())	Index = Index % m_Buttons.size();
			
			//Draws button
			((Button)m_Buttons.elementAt(Index)).setX(m_X);
			((Button)m_Buttons.elementAt(Index)).setY(m_Y + (24 * (i - Start)));
			((Button)m_Buttons.elementAt(Index)).draw(g);
		}
		
		//Not drawn anymore
		m_Draw = false;
	}
	
	//Data
	protected int		m_X;
	protected int		m_Y;
	protected int		m_Limit;
	protected int		m_Index;
	protected int		m_Selected;
	protected Image		m_Selection1;
	protected Image		m_Selection2;
	protected Vector	m_Buttons;
	protected boolean 	m_Draw;
}
